package com.cmsz.xpay.common.util;

import java.util.Map;

/**
 * RSA签名验签工具类
 * @author liyuanchang 
 * 2016.4.6
 */
public class RSASignUtil {

	private static final String UTF_8 = "UTF-8";

	/**
	 * RSA签名
	 * 
	 * @param str
	 *            待签名字符串
	 * @param privateKey
	 *            私钥
	 * @param charsetName
	 *            字符集
	 * @return
	 */
	public static String sign(String str, String privateKey, String charsetName) {
		String sign = RSAUtil.sign(str, privateKey, charsetName);
		return sign;
	}

	/**
	 * RSA签名，字符集为UTF-8
	 * 
	 * @param str
	 *            待签名字符串
	 * @param privateKey
	 *            私钥
	 * @return
	 */
	public static String sign(String str, String privateKey) {
		return sign(str, privateKey, UTF_8);
	}

	/**
	 * 将map中元素的key值按升序排序为"key1=value1&key2=value2"的方式进行RSA签名
	 * 
	 * @param map
	 *            待签名map
	 * @param privateKey
	 *            rsa私钥
	 * @param charsetName
	 *            字符集
	 * @return
	 */
	public static String sign(Map<String, String> map, String privateKey, String charsetName) {
		String sign = "";
		String signTempStr = KeyValueUtil.mapToString(map);
		sign = sign(signTempStr, privateKey, charsetName);
		return sign;
	}

	/**
	 * 将map中元素的key值按升序排序为"key1=value1&key2=value2"的方式进行RSA签名，字符集为UTF-8
	 * 
	 * @param map
	 *            待签名map
	 * @param privateKey
	 *            rsa私钥
	 * @return
	 */
	public static String sign(Map<String, String> map, String privateKey) {
		return sign(map, privateKey, UTF_8);
	}

	/**
	 * 将map中元素的key值按升序排序为"key1=value1&key2=value2"的方式进行RSA验签，字符集为UTF-8
	 * 
	 * @param str
	 *            待验签字符串
	 * @param sign
	 *            验签签名
	 * @param publicKey
	 *            rsa公钥
	 * @param charsetName
	 *            字符集
	 * @return true:验签成功，false:验签失败
	 */
	public static boolean verify(String str, String sign, String publicKey, String charsetName) {
		boolean flag = RSAUtil.verify(str, sign, publicKey, charsetName);
		return flag;
	}

	/**
	 * 将map中元素的key值按升序排序为"key1=value1&key2=value2"的方式进行RSA验签
	 * 
	 * @param str
	 *            待验签字符串
	 * @param sign
	 *            验签签名
	 * @param publicKey
	 *            rsa公钥
	 * @return true:验签成功，false:验签失败
	 */
	public static boolean verify(String str, String sign, String publicKey) {
		return verify(str, sign, publicKey, UTF_8);
	}

	/**
	 * RSA验签，字符集为UTF-8
	 * 
	 * @param map
	 *            待验签map
	 * @param sign
	 *            验签签名
	 * @param publicKey
	 *            rsa公钥
	 * @param charsetName
	 *            字符集
	 * @return true:验签成功，false:验签失败
	 */
	public static boolean verify(Map<String, String> map, String sign, String publicKey, String charsetName) {
		String verfiyTempStr = KeyValueUtil.mapToString(map);
		return verify(verfiyTempStr, sign, publicKey, charsetName);
	}

	/**
	 * RSA验签，字符集为UTF-8
	 * 
	 * @param map
	 *            待验签map
	 * @param sign
	 *            验签签名
	 * @param publicKey
	 *            支付宝分配的rsa公钥
	 * @return true:验签成功，false:验签失败
	 */
	public static boolean verify(Map<String, String> map, String sign, String publicKey) {
		return verify(map, sign, publicKey, UTF_8);
	}
}
